package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.MachineAccountBean
import com.hinnotek.warehouse.databinding.ActivityNonceBorrowingBinding
import com.hinnotek.warehouse.databinding.ItemBorrowApplyLayoutBinding
import com.hinnotek.warehouse.dialog.Dialogs
import com.hinnotek.warehouse.utils.WAREHOUSE_TYPE_2
import com.hinnotek.warehouse.utils.WAREHOUSE_TYPE_3
import com.hinnotek.warehouse.viewmodel.StockInViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.*
import com.lee.common.utils.ACTIVITY_WAREHOUSE_STOCK_OUT_LIST
import dagger.hilt.android.AndroidEntryPoint

/**
 * 临时借用
 */
@Route(path = ACTIVITY_NONCE_BORROWING)
@AndroidEntryPoint
class NonceBorrowingActivity : BaseActivity<ActivityNonceBorrowingBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineAccountBean, ItemBorrowApplyLayoutBinding>
    private val viewModel by viewModels<StockInViewModel>()
    private val applyType = WAREHOUSE_TYPE_3

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_nonce_borrowing)
        setTitle("临时借用")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.applyType = applyType
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()
    }

    override fun initVew() {
        adapter =
            SimpleBindingAdapter(R.layout.item_borrow_apply_layout, ArrayList(), BR.borrowApplyData)
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }
        //添加adapter
        adapter.addListener { binding, item ->
            //查看详情
            binding.root.setOnClickListener {
                Skipping.start(
                    ACTIVITY_NONCE_BORROWING_LIST, "applyId",
                    item.applyId, MACHINE_CONTENT_LIST,
                    item, this@NonceBorrowingActivity
                )
            }
        }
        //新增跳转事件
        setRightText(getString(R.string.addwarehouse))?.setOnClickListener {
            Skipping.start(
                ACTIVITY_WAREHOUSE_NEW_STOCK_IN,
                "applyType",
                applyType,
                this@NonceBorrowingActivity
            )
        }
        //搜索点击事件
        bindingView.search.setOnClickListener {
            viewModel.setParameters(bindingView.topView.text.toString())
            viewModel.refresh()
        }
    }

    /**
     * 发送审核数据
     */
    private fun toApproval(state: Int, applyId: Int) {
        viewModel.approvalState = state
        viewModel.approvalApplyId = applyId
        viewModel.refusalReasons = ""
        viewModel.loadData()
    }

    override fun setData() {
        //加载列表数据
        viewModel.stockInPage.observe(this, Observer {
            adapter.addAll(it, viewModel.isFirst)
        })
        //审核接口监听
        viewModel.approvalData.observe(this, Observer {
            if (viewModel.isSuccess) {
                viewModel.refresh()
            }
        })
    }

}